<?php

namespace App\Http\Middleware;
use DB;
use Closure;

class AdminLoginMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if($request -> session() -> has('id')){
            $id = session('id');
            $res = DB::table('userdetail') -> where('id',$id)->first();
            if($res){
                session([
                       'name' =>$res -> username, 
                       'photo' => $res -> photo,
                    ]);
            }else{
                session() -> forget('id');
            }
            return $next($request);
        }else{
            return redirect('/admin/login'); 
        }
        
    }
}
